#include"game.h"

int x = 0;
int y = 0;

void menu()
{
  printf("####################################\n");
  printf("#####     1.play    0,exit     #####\n");
  printf("####################################\n");
  printf("Please select#");
}

void ShowBoard(int board[][COL])
{
  printf("\e[1;1H\e[2J");//格式清屏
  printf("  ");

  int i = 0;
  for(i = 0; i < COL; i++)
  {
    printf("%3d", i);
  }
  printf("\n");

  for(i = 0; i < ROW; i++)
  {
    printf("%2d ", i);
    int j = 0;
    for(j = 0; j < COL; j++)
    {
      if(board[i][j] == 0)
      {
        printf(" . ");
      }
      else if(board[i][j] == PLAYER1)
      {
        printf(" x ");
      }
      else 
      {
        printf(" o ");
      }
    }
    printf("\n");
  }
}

void PlayerMove(int board[][COL], int who)
{
  while(1)
  {
    printf("Please player[%d] enter your pos#", who);
    scanf("%d %d", &x, &y);
    if(x < 0 || x > ROW - 1 || y < 0 || y > COL - 1)
    {
      printf("位置非法，请重新输入\n");
    }
    else if(board[x][y] != 0)
    {
      printf("改位置已被占用，请重新输出\n");
    }
    else 
    {
      board[x][y] = who;
      break;
    }

  }
}

int ChessCount(int board[][COL], enum dir d)
{
  int _x = x;
  int _y = y;
  int count = 0;

  while(1)
  {
    switch(d)
    {
      case LEFT:
        _y--;
        break;
      case RIGHT:
        _y++;
        break;
      case UP:
        _x--;
        break;
      case DOWN:
        _x++;
        break;
      case LEFT_UP:
        _x--,_y--;
        break;
      case RIGHT_DOWN:
        _x++,_y++;
        break;
      case LEFT_DOWN:
        _x++,_y--;
        break;
      case RIGHT_UP:
        _x--,_y++;
        break;
      default:
        //do nothing
        break;
    }

    if(_x < 0 || _x > ROW - 1 || _y < 0 || _y > COL - 1)
    {
      return count;
    }

    if(board[_x][_y] != board[x][y])
    {
      return count;
    }
    else 
    {
      count++;
    }
  }
}

int IsOver(int board[][COL])
{
  int count1 = ChessCount(board, LEFT) + ChessCount(board, RIGHT) + 1;
  int count2 = ChessCount(board, UP) + ChessCount(board, DOWN) + 1;
  int count3 = ChessCount(board, LEFT_UP) + ChessCount(board, RIGHT_DOWN) + 1;
  int count4 = ChessCount(board, LEFT_DOWN) + ChessCount(board, RIGHT_UP) + 1;
  
  if(count1 >= 5 || count2 >= 5 || count3 >=5 || count4 >= 5)
  {
    return board[x][y];
  }

  int i = 0;
  for(i = 0; i < ROW; i++)
  {
    int j = 0;
    for(j = 0; j < COL; j++)
    {
      if(board[i][j] == 0)
      {
        return NEXT;
      }
    }
  }

  return DRAW;
}


void game()
{
  int board[ROW][COL];
  memset(board, 0, sizeof(board));
  int result = NEXT;
  do 
  {
    ShowBoard(board);
    PlayerMove(board, PLAYER1);
    result = IsOver(board);
    if(result != NEXT)
    {
      break;
    }

    ShowBoard(board);
    PlayerMove(board, PLAYER2);
    result = IsOver(board);
    if(result != NEXT)
    {
      break;
    }

  }while(1);

  ShowBoard(board);

  switch(result)
  {
    case PLAYER1_WIN:
      printf("恭喜玩家1，您赢了！\n");
      break;
    case PLAYER2_WIN:
      printf("恭喜玩家2，您赢了！\n");
      break;
    case DRAW:
      printf("平局。\n");
      break;
    default:
      //do nothing
      break;
  }
}
